﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ShowRS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DisplaySR();
    }

    private void DisplaySR()
    {
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from SpecialRecharge", DataProcess.ConnectionString);
            DataSet ds = new DataSet();
            da.Fill(ds, "SR");
            grvSR.DataSource = ds.Tables["SR"];
            grvSR.DataBind();
        }
        catch (Exception ex)
        {
            ex.StackTrace.ToString();
        }
    }


    protected void grvSR_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        if (e.Row.RowIndex != grvSR.EditIndex)
        {
            ((ImageButton)e.Row.Cells[6].FindControl("imgDel")).Attributes.Add("onclick", "return confirm('Are you sure to delete this package')");
        }
    }

    protected void grvSR_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvSR.EditIndex = -1;
        DisplaySR();
    }
    protected void grvSR_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvSR.EditIndex = e.NewEditIndex;
        DisplaySR();
    }
    protected void grvSR_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id_SP = int.Parse(grvSR.DataKeys[e.RowIndex].Value.ToString());
        string _packageName = ((TextBox)grvSR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        int _durationDate = int.Parse(((TextBox)grvSR.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
        int _discount = int.Parse(((TextBox)grvSR.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        float _denominations = int.Parse(((TextBox)grvSR.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
        string _description = ((TextBox)grvSR.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

        SqlCommand cmd = DataProcess.Connection.CreateCommand();
        cmd.CommandText = "update SpecialRecharge set _packageName=@_packageName, _durationDate=@_durationDate, _discount=@_discount, _denominations=@_denominations, _description=@_description where id_SP=@id_SP";
        cmd.Parameters.Add(new SqlParameter("@id_SP", id_SP));
        cmd.Parameters.Add(new SqlParameter("@_packageName", _packageName));
        cmd.Parameters.Add(new SqlParameter("@_durationDate", _durationDate));
        cmd.Parameters.Add(new SqlParameter("@_discount", _discount));
        cmd.Parameters.Add(new SqlParameter("@_denominations", _denominations));
        cmd.Parameters.Add(new SqlParameter("@_description", _description));
        cmd.ExecuteNonQuery();
        grvSR.EditIndex = -1;
        DisplaySR();
    }
    protected void grvSR_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id_SP = int.Parse(grvSR.DataKeys[e.RowIndex].Value.ToString());
        DeleteSR(id_SP);
        DisplaySR();
    }

    private void DeleteSR(int id_SP)
    {
            SqlCommand cmd = DataProcess.Connection.CreateCommand();
            cmd.CommandText = "delete from SpecialRecharge where id_SP=@id_SP";
            cmd.Parameters.Add(new SqlParameter("@id_SP", id_SP));
            cmd.ExecuteNonQuery();
    }
}